package apachePoi;
import leihua.crm.commons.utils.HSSFUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
 * 解析excel文件
 *
 * @Author leiHua
 * @Date 2022/10/14 12:31
 */
public class ParseExcelDemo {
    public static void main(String[] args) throws IOException {
        //将excel文件 转换成workbook对象
        InputStream is = new FileInputStream("F:\\资源\\demo\\students.xls");
        HSSFWorkbook workbook = new HSSFWorkbook(is);
        //将excel文件中第一页信息封装成SheetAt对象
        HSSFSheet sheet = workbook.getSheetAt(0);
        HSSFRow row = null;
        HSSFCell cell = null;

        for (int i = 0; i <= sheet.getLastRowNum(); i++) {  //getLastRowNum:  获取该页中最后一行的下标
            row = sheet.getRow(i);
            //遍历每行中的列
            for (int j = 0; j < row.getLastCellNum(); j++) {
                cell = row.getCell(j);
//                if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {  //如果类型是boolean
//                    System.out.print(cell.getBooleanCellValue() + " ");
//                } else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) { //如果是公式类型
//                    System.out.print(cell.getCellFormula() + " ");
//                } else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {  //如果是字符串类型的
//                    System.out.print(cell.getStringCellValue() + " ");
//                } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {  //数字类型
//                    System.out.print(cell.getNumericCellValue() + " ");
//                } else {
//                    System.out.print("" + " ");
//                }
                String str=HSSFUtils.getCellValueForString(cell);
                System.out.print(str);
            }
            System.out.println(); //换行
        }
    }
}
